// Matrix addition, CPU version
// gcc matrix_cpu.c -o matrix_cpu -std=c99

#include <stdio.h>
#include "milli.h"

void add_matrix(float *a, float *b, float *c, int N)
{
  int index,i,j;
	
	for (i = 0; i < N; i++)
		for (j = 0; j < N; j++)
		{
			index = i + j*N;
			c[index] = a[index] + b[index];
		}
}

int main()
{
	const int N = 32;
	int i,j;
	//float a[N*N];
	//float b[N*N];
	//float c[N*N];

	float *a = (float *) malloc(sizeof(float)*N*N);
	float *b = (float *) malloc(sizeof(float)*N*N);
	float *c = (float *) malloc(sizeof(float)*N*N);

	for (i = 0; i < N; i++)
	  for (j = 0; j < N; j++)
	    {
	      a[i+j*N] = 10 + i;
	      b[i+j*N] = (float)j / N;
	    }
	
	int start,stop;
	start = GetMicroseconds();
	add_matrix(a, b, c, N);
	stop = GetMicroseconds();
	
	for (i = 0; i < N; i++)
	  {
	    for (j = 0; j < N; j++)
	      {
		printf("%0.2f ", c[i+j*N]);
	      }
	    printf("\n");
	}
	
	free(a);
	free(b);
	free(c);
	printf("time: %0.3f ms\n", ((float)(stop - start))/1000);
  
}
